import {
	loopItem,
	findItemByKey
} from '../../../utils/tree.js';
import {
	buildShortUUID
} from '../../../utils/uuid.js';
import {
	ref,
	provide
} from "vue";
import {
	cloneData
} from '@/sl-uni-sdk/utils/common.js';

export function useFormItem({
	dyFormData
}) {
	const formItemExposeObj = {};
	
	function setRelateValue(data, dyFormItem, relateObj){
		for (let key in data) {
			if (relateObj[key] === dyFormItem.field && data[key] !== undefined) {
				dyFormItem.value = data[key];
				break;
			}
		}
	}
	provide("useFormItem", {
		setExpose(obj) {
			const key = buildShortUUID();
			formItemExposeObj[key] = obj;
			return key;
		},
		removeExpose(key) {
			formItemExposeObj[key] = null;
		},
		setValueByRelateField(data, relateField, uuid) {
			//[{key1: value1},{key2: value2}......] ===> {key1: value1,key2: value2.....}
			const relateObj = {};
			relateField.forEach((el) => {
				Object.assign(relateObj, el);
			});
			if (uuid) {
				loopItem(dyFormData.value, (item, i, items) => {
					if (item.uuid === uuid) {
						items.forEach((item) => {
							setRelateValue(data, item, relateObj);
						});
						return false;
					}
				});
			} else {
				dyFormData.value.forEach((el) => {
					if (el.compType !== "eqGroups") {
						if(el.children && el.children.length > 0) {
							el.children.forEach((item) => {
								setRelateValue(data, item, relateObj);
							});
						} else {
							setRelateValue(data, el, relateObj);
						}
					}
				});
			}
		},
	});

	function verifyAll() {
		for (let key in formItemExposeObj) {
			const obj = formItemExposeObj[key];
			if (obj) {
				const re = obj.verify();
				if (!re) return false;
			}
		}
		return true;
	}
	async function excuteAll() {
		for (let key in formItemExposeObj) {
			const obj = formItemExposeObj[key];
			if (obj) {
				await obj.excute();
			}
		}
	}
	async function submit() {
		if (verifyAll()) {
			await excuteAll();
			return true;
		} else {
			return false;
		}
	}

	function toJSONObj() {
		let eqGroupsItem;
		const obj = {};
		dyFormData.value.forEach((el) => {
			if (el.compType === "eqGroups") {
				eqGroupsItem = el;
			} else if (el.children && el.children.length > 0) {
				el.children.forEach((item) => {
					obj[item.field] = item.value;
				});
			} else {
				obj[el.field] = el.value;
			}
		});
		if (eqGroupsItem) {
			const eqArr = [];
			eqGroupsItem.children.forEach((item) => {
				if (item.compType === 'eqGroup') {
					const eqObj = {};
					item.children.forEach((item) => {
						eqObj[item.field] = item.value;
					});
					eqArr.push(eqObj);
				} else {
					obj[item.field] = item.value;
				}
			});
			obj.faultEquipments = eqArr;
		}
		console.log("=======>toJSONObj", obj);
		return obj;
	}

	function setJSONObj(obj) {
		console.log("=======>setJSONObj", obj);
		dyFormData.value.forEach((rootEl) => {
			if (rootEl.compType === "eqGroups") {
				let copyEqGroup = null;
				for (let i = rootEl.children.length - 1; i >= 0; i--) {
					const item = rootEl.children[i];
					if (item.compType === "eqGroup") {
						if (copyEqGroup === null) {
							copyEqGroup = cloneData(item);
						}
						rootEl.children.splice(i, 1);
					} else {
						item.value = obj[item.field];
					}
				}
				if (obj.faultEquipments && copyEqGroup) {
					obj.faultEquipments.forEach((eqItem) => {
						let newEqGroup = cloneData(copyEqGroup);
						newEqGroup.uuid = buildShortUUID();
						newEqGroup.children.forEach((item) => {
							item.value = eqItem[item.field];
						});
						// console.log(newEqGroup, eqItem);
						rootEl.children.push(newEqGroup);
					});
				}
			} else if(rootEl.children && rootEl.children.length > 0){
				rootEl.children.forEach((item) => {
					if (obj[item.field] !== undefined) {
						item.value = obj[item.field];
					}
				});
			} else {
				rootEl.value = obj[rootEl.field];
			}
		});
	}
	return {
		verifyAll,
		submit,
		toJSONObj,
		setJSONObj,
	};
}